package com.lam.dormitory.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lam.dormitory.common.quartz.JobUtil;
import com.lam.dormitory.entity.Quartz;
import com.lam.dormitory.service.IQuartzService;
import com.lam.dormitory.util.ResponseTemplate;
import com.lam.dormitory.vo.QuartzVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Api(tags = "定时任务")
@RestController
@RequestMapping("/quartz")
public class QuartzController {
    @Autowired
    private JobUtil jobUtil;
    @Autowired
    private IQuartzService quartzService;


    //添加一个job
    @ApiOperation(value = "添加任务", notes = "根据条件jobName,jobGroup,cronExpression")
    @PostMapping("/addJob")
    @RequiresRoles(value = {"superadmin", "admin", "student"}, logical = Logical.OR)
    public ResponseTemplate addJob(Quartz quartz) throws Exception {
        QueryWrapper<Quartz> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Quartz::getJobName, quartz.getJobName()).eq(Quartz::getJobGroup, quartz.getJobGroup());
        Quartz one = quartzService.getOne(wrapper);
        if (one == null) {
            quartzService.save(quartz);
        }
        return jobUtil.addJob(quartz);
    }

    //暂停job
    @ApiOperation(value = "暂停任务", notes = "根据条件quartzId")
    @PostMapping("/pauseJob")
    @RequiresRoles(value = {"superadmin", "admin", "student"}, logical = Logical.OR)
    public ResponseTemplate pauseJob(Long quartzId) throws Exception {
        QueryWrapper<Quartz> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Quartz::getQuartzId, quartzId);
        Quartz one = quartzService.getOne(wrapper);

        QueryWrapper<Quartz> quartzQueryWrapper = new QueryWrapper<>();
        quartzQueryWrapper.lambda().eq(Quartz::getQuartzId, one.getQuartzId());
        Quartz quartz = new Quartz();
        quartz.setStatus(0);
        quartzService.update(quartz, quartzQueryWrapper);
        jobUtil.pauseJob(one.getJobName(), one.getJobGroup());
        return new ResponseTemplate(200, "success pauseJob");
    }

    //恢复job
    @ApiOperation(value = "恢复任务", notes = "根据条件quartzId")
    @PostMapping("/resumeJob")
    @RequiresRoles(value = {"superadmin", "admin", "student"}, logical = Logical.OR)
    public ResponseTemplate resumeJob(Long quartzId) throws Exception {
        QueryWrapper<Quartz> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Quartz::getQuartzId, quartzId);
        Quartz one = quartzService.getOne(wrapper);
        QueryWrapper<Quartz> quartzQueryWrapper = new QueryWrapper<>();
        quartzQueryWrapper.lambda().eq(Quartz::getQuartzId, one.getQuartzId());
        Quartz quartz = new Quartz();
        quartz.setStatus(1);
        quartz.setStartTime(new Date());
        quartzService.update(quartz, quartzQueryWrapper);
        jobUtil.resumeJob(one.getJobName(), one.getJobGroup());
        return new ResponseTemplate(200, "success resumeJob");
    }


    //删除job
    @ApiOperation(value = "删除任务", notes = "根据条件quartzId")
    @PostMapping("/deleteJob")
    @RequiresRoles(value = {"superadmin", "admin", "student"}, logical = Logical.OR)
    public ResponseTemplate deleteJob(Long quartzId) throws Exception {
        QueryWrapper<Quartz> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Quartz::getQuartzId, quartzId);
        Quartz quartz = quartzService.getOne(wrapper);
        ResponseTemplate responseTemplate = jobUtil.deleteJob(quartz);
        if (responseTemplate.getCode() == 200) {
            quartzService.removeById(quartzId);
        }
        return new ResponseTemplate(200, "success deleteJob");
    }

    //修改
    @ApiOperation(value = "修改任务", notes = "根据条件quartzId,jobName,jobGroup,cronExpression")
    @PostMapping("/updateJob")
    @RequiresRoles(value = {"superadmin", "admin", "student"}, logical = Logical.OR)
    public ResponseTemplate modifyJob(Quartz quartz) throws Exception {
        System.out.println(quartz.getInvokeParam());
        ResponseTemplate responseTemplate = jobUtil.modifyJob(quartz);
        if (responseTemplate.getCode() == 200) {
            QueryWrapper<Quartz> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(Quartz::getQuartzId, quartz.getQuartzId());
            boolean update = quartzService.update(quartz, wrapper);
            if (update) {
                return new ResponseTemplate(200, "success updateJob", responseTemplate.getData());
            }
        } else {
            return new ResponseTemplate(500, "fail updateJob", responseTemplate.getData());
        }
        return null;
    }

    //暂停所有
    @ApiOperation(value = "暂停所有任务", notes = "")
    @PostMapping("/pauseAll")
    @RequiresRoles(value = {"superadmin", "admin", "student"}, logical = Logical.OR)
    public ResponseTemplate pauseAllJob() throws Exception {
        return jobUtil.pauseAllJob();
    }

    //恢复所有
    @ApiOperation(value = "恢复所有任务", notes = "")
    @PostMapping("/resumeAllJob")
    @RequiresRoles(value = {"superadmin", "admin", "student"}, logical = Logical.OR)
    public ResponseTemplate resumeAllJob() throws Exception {
        return jobUtil.resumeAllJob();
    }

    //获取任务状态
    @ApiOperation(value = "获取Job状态", notes = "")
    @PostMapping("/getJobState")
    @RequiresRoles(value = {"superadmin", "admin", "student"}, logical = Logical.OR)
    public ResponseTemplate getJobState(Long quartzId) throws Exception {
        QueryWrapper<Quartz> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Quartz::getQuartzId, quartzId);
        Quartz quartz = quartzService.getOne(wrapper);
        return jobUtil.getJobState(quartz.getJobName(), quartz.getJobGroup());
    }

    @ApiOperation(value = "获取Job列表", notes = "根据条件")
    @PostMapping("/list")
    @RequiresRoles(value = {"superadmin", "admin", "student"}, logical = Logical.OR)
    public ResponseTemplate getJobList(Quartz quartz, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "0") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<QuartzVo> quartzList = quartzService.selectQuartz(quartz);
        PageInfo<QuartzVo> pageInfo = new PageInfo<>(quartzList);
        return new ResponseTemplate(200, "获取成功", quartzList, pageInfo.getTotal());
    }

}